﻿var leftPage = 1;
var leftIndex = 0;
var page = 1;
disajax = false;
var mainIndex = null;
var runState = false;
var runParamName = ''
var MyMar;
// 文字走馬燈
function timedCount(index, str) {
    var elem = str;
    var thisContain = elem[index];
    console.info(thisContain);
    var thisIntro = elem[index].getElementsByClassName(runParamName);
    thisIntro[0].style.left = '0px';
    if (detailone) {
        thisContain = $(".dlist-title")[index];
    }

    function Marquee() {
        var left = thisIntro[0].style.left;
        if (thisContain.offsetWidth < thisIntro[0].offsetWidth) {
            if (parseInt(left) > -thisIntro[0].offsetWidth) {
                thisIntro[0].style.left = (parseInt(left) - 1) + 'px';
            } else {
                thisIntro[0].style.left = '0px'
            }
        }
    }
    MyMar = setInterval(Marquee, 40);
}

function MessOpt(page, totalPage, pos, index, run, paramName) {

    if (run == 'run' && paramName && runState) { //文字走馬燈
        // runState = true;
        runParamName = paramName;
    }

    $('#now_page').html(page);
    $('#total_page').html(totalPage);

    var rightIndex = (typeof index) == 'number' ? index : pos == 'next' ? 0 : modelIndex.pageSize - 1;

    right.initialization();
    if (!(pos == 'create' && !(typeof index == 'number'))) {
        //		debugger
        right.toggleFocus(rightIndex);
    }

}

function modelInit() {
    var indexList = new TV.CookieOpt().getOpt()
    if (indexList && JSON.stringify(indexList) != "{}") {
        page = indexList.page;
        mainIndex = indexList.index;
        getMenus(indexList.leftPage, indexList.leftIndex)
    } else {
        getMenus();
        cmsIndex.handle('left');
    }
    if (!modelIndex.disUser) {
        TV.UserMess()
    }
    if (!modelIndex.disTitle) {
        //      TV.getHeader();
    }
}

/*
 * 0.导航内容
 */
cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'left',
    modNum: cmsIndex.moduleGuid(),
    map: {
        right: 1,
        menulist: 2
    }
};

var left = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function() {
    var mod = {
            modName: 'menuList',
            modNum: cmsIndex.moduleGuid()
        },
        index = modelIndex.index,
        type = modelIndex.structure || 1,
        pageSize = modelIndex.menuPageSize,
        nowPage = 1,
        totalPage = 1,
        length = 0,
        containerId = modelIndex.menuContainerId,
        listName = modelIndex.menuListName;

    //左侧内容生成
    mod.paint = function(page, number) {

        if (page != undefined && index != undefined) {
            nowPage = page;
            index = number;
        }

        var me = mod;
        // console.log(leftData)
        var json = new TV.pageListOpt(leftData, pageSize, nowPage, listName, true);

        totalPage = json.totalPage;
        length = json.length;
        $('#' + containerId).html(json.str);
        me.toggleFocus();
        return me;

    };

    //动态操作
    mod.toggleFocus = function() {
        var me = mod;
        elem = $('#' + containerId).children('.' + listName);
        for (i = 0; i < length; i++) {
            // debugger;
            elem[i].className = listName;
            elem[i].children[0].src = "images/dtindex/ellipse.png";

        }
        if (index >= 0 && index <= length - 1) {
            elem[index].className = elem[index].className + ' focus_menu';
            elem[index].children[0].src = "images/dtindex/ellipseSelect.png";

        }
        if (elem[index].getAttribute('data-status')) {
            menulist.paint();
            modelIndex.menuIndex = 0;
            //menulist.initialization();
        } else {
            $('#menuContent').html('')
        }
        me.initialization();
        return me;
    };
    //赋值
    mod.initialization = function() {
        var me = mod;
        elem = $('#' + containerId).children('.' + listName);
        var id = elem[index].getAttribute('data-id');
        modelIndex.type = elem[index].getAttribute('type_style');
        modelIndex.mainlistName = elem[index].getAttribute('list_name');
        modelIndex.pageSize = parseInt(elem[index].getAttribute('data_size'));
        modelIndex.api = elem[index].getAttribute("api");
        modelIndex.status = elem[index].getAttribute("status");
        modelIndex.category = elem[index].getAttribute("category");
        modelIndex.is_volunteer = elem[index].getAttribute("is_volunteer");
        modelIndex.is_menu = elem[index].getAttribute("is_menu");

        leftPage = nowPage;
        leftIndex = index;
        getMessage(id, page, 'create', mainIndex, modelIndex.type);
        mainIndex = null;
        page = 1;
        return me;
    };
    //操作
    mod.moveOp = function(directive) {

        var me = mod;
        var isContent = false
        if ($('#' + modelIndex.mainContainerId).children("." + modelIndex.mainlistName).length) {
            isContent = true
        }
        menuCu = {
            directive: directive,
            type: type,
            isContent: isContent,
            modelType: modelIndex.type,
            index: index,
            modelName: 'right',
            nowPage: nowPage,
            totalPage: totalPage,
            length: length,
            pageSize: pageSize
        }

        var json = new TV.MenuOperation(menuCu).operation();
        console.log(json)
        if (json) {
            if (json.backRoute) {
                result = cmsIndex.handle(json.backRoute);
                result.toggleFocus(0);
                return;
            }
            index = json.index;
            nowPage = json.nowPage;
            if (json.flag) {
                me.paint();
            } else {
                me.toggleFocus();
            }
        }

        return me;
    };

    //电视操作
    mod.action = function(directive) {
        var me = mod;
        switch (directive) {
            case 'enter':
                var data_id = $(".focus_menu").eq(0).attr("data-id")
                if (data_id == "264") {
                    window.location.href = "http://172.31.183.147:8080/webapp_v2/tbkt/index.html";
                }
                break;
            case 'back':
                if (modelIndex.disModel) {
                    Utility.ioctlWrite("skyworthKey", "Key2VOD");
                } else {
                    history.go(-1);
                }
                break;
        }
        return me;
    };

    mod.grabEvent = function(key) {
        var me = mod;
        var keyJson = TV.GetKey(key);
        if (keyJson.keyState && keyJson.keyState != 'enter' && keyJson.keyState != 'back') {
            me.moveOp(keyJson.keyState);
        } else {
            me.action(keyJson.keyState)
        }
        return keyJson.isBlock;
    };
    return mod;
})();

/*
 * 1.右侧区
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'right',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 0,
        menulist: 2
    }
};

var right = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function() {
    var mod = {
            modName: "pageList",
            modNum: cmsIndex.moduleGuid()
        },
        index = 0, //位置
        cleanFlag = 0,
        isLike = modelIndex.isLike, //是否开启点赞功能
        structure = modelIndex.structure,
        status = '1', //是否在外框
        length = 0, //真实长度
        nowPage = 1, //当前页数
        totalPage = 1, //总页数
        type = '', //类型
        listName = '', //模块名
        pageSize = 1, //模块数量
        rowHeight = modelIndex.rowHeight, //多行模块行数
        containerId = modelIndex.mainContainerId; //包含模块的标签id

    mod.cleanCSS = function() {

            var elem = $("#" + containerId).children("." + listName);
            var elemCur = $("#" + containerId).children(".cur")
            for (i = 0; i < length; i++) {
                elem[i].className = listName;
                if (elemCur.length) {
                    elemCur[i].className = 'cur';
                }

            }
        }
        //操作
    mod.toggleFocus = function(number) {
        var me = mod;
        if (number != undefined) {
            index = number;
        }
        if (type == '0') {
            // var elem = $("#" + containerId) //原实践岗站的写法

            var elem = $("#" + containerId).children("." + listName);

            elem[index].className = elem[index].className + ' active';
        } else {
            var elem = $("#" + containerId).children("." + listName);
            var elemCur = $("#" + containerId).children(".cur")
            for (i = 0; i < length; i++) {
                elem[i].className = listName;
                if (elemCur.length) {
                    elemCur[i].className = 'cur';
                }
            }
            if (status == '0') {
                elemCur[index].className = elemCur[index].className + ' curActive'
            }
            if (index >= 0 && index < length) {
                elem[index].className = elem[index].className + ' active';
                // 走馬燈
                if (runState == true) {
                    clearInterval(MyMar);
                    timedCount(index, elem);
                }
            }
            if (cleanFlag) {
                cleanFlag = 0;
                me.cleanCSS();
            }
        }

        return me;
    };
    //赋值
    mod.initialization = function() {
        var me = mod;
        listName = modelIndex.mainlistName;
        pageSize = modelIndex.pageSize;
        containerId = modelIndex.mainContainerId;
        nowPage = parseInt(document.getElementById('now_page').innerHTML);
        totalPage = parseInt(document.getElementById('total_page').innerHTML);
        length = $('#' + containerId).children("." + listName).length;
        type = modelIndex.type;
        return me;
    };
    //index 位置调整
    mod.moveOp = function(directive) {
        var me = mod;
        // console.log(type)
        if (modelIndex.type == '0') {
            console.log(directive)
            if (directive == 'down') {
                TV.articleMove(directive, modelIndex.thisContain, modelIndex.thisDetail);
            } else if (directive == 'up') {
                TV.articleMove(directive, modelIndex.thisContain, modelIndex.thisDetail);
            } else if (directive == 'backwards') {
                console.log()
                clearInterval(articleRun);
                var elem = $("#" + containerId);
                elem[index].className = containerId;
                result = cmsIndex.handle('left');
                result.toggleFocus(1);
            }
        } else {
            var json = new TV.ListOperation({
                type: type,
                direction: 'left',
                structure: structure,
                key: directive,
                isLike: isLike,
                isMenu: $('.focus_menu')[0].getAttribute('data-status') ? 1 : 0,
                status: status,
                nowPage: nowPage,
                totalPage: totalPage,
                pageSize: pageSize,
                length: length,
                index: index,
                rowHeight: rowHeight
            })

            var message = json.operation();
            console.info(message)
            index = message.index;
            status = message.status;
            if (message.position != '') {
                var id = $('.focus_menu').attr('data-id');
                getMessage(id, message.nowPage, message.position); //下一页
            }
            if (message.direction) {
                var cms = cmsIndex.handle(message.direction);
                if (message.direction == 'menulist') {
                    cms.toggleFocus();
                }
                cleanFlag = 1;

            }
            mod.toggleFocus();
            return me;
        }

    };
    mod.action = function(directive) {
        var me = mod;
        switch (directive) {
            case 'back':

                if (nowPage != 1) {
                    document.getElementById('now_page').innerHTML = 1
                    nowPage = 2
                    index = 0
                    mod.moveOp("backwards")
                    setTimeout(function() {
                        right.toggleFocus(0)

                    }, 200)
                } else {
                    history.go(-1);
                }
                break;
            case 'enter':
                // console.log(nowPage, index, leftPage, leftIndex)
                if (isCache) {
                    var cookieTv = new TV.CookieOpt({
                        index: index,
                        page: nowPage,
                        leftPage: leftPage,
                        leftIndex: leftIndex
                    })
                    var indexList = cookieTv.setOpt();
                }

                var id = $('.focus_menu').attr('data-id');
                direEnter(status, id, index);

                break;
        }
        return me;
    };
    mod.grabEvent = function(key) {
        var me = mod;
        var keyJson = TV.GetKey(key);
        if (keyJson.keyState && keyJson.keyState != 'enter' && keyJson.keyState != 'back') {
            me.moveOp(keyJson.keyState);
        } else {
            me.action(keyJson.keyState)
        }
        return keyJson.isBlock;

    };

    return mod;
})();

/* 
 * 这是一个额外加的导航栏 fuck
 */
cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'menulist',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 0,
        right: 1
    }
};

var menulist = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function() {
    var mod = {
            modName: "menuList",
            modNum: cmsIndex.moduleGuid()
        },
        listName = 'menuList', //模块名
        containerId = "menuContent", //区域名
        length = muenListData.length,
        index = modelIndex.menuIndex;

    mod.cleanCSS = function() {
            var elem = $("#" + containerId).children("." + listName);
            for (i = 0; i < length; i++) {
                elem[i].className = listName;
            }
        }
        //导航内容生成
    mod.paint = function(page, number) {
        // console.log('我这里还有个模块')
        if (page != undefined && index != undefined) {
            nowPage = page;
            index = number;
        }
        var str = ''
        for (var i = 0; i < muenListData.length; i++) {
            str += '<li data-id=' + muenListData[i].id + ' class=' + listName + ' data_name=' + muenListData[i].name + '>' + muenListData[i].name + '</li>'
        }
        $('#' + containerId).html(str);

    };
    //动态操作
    mod.toggleFocus = function() {
            var me = mod;
            elem = $('#' + containerId).children('.' + listName);
            for (i = 0; i < length; i++) {
                // debugger;
                elem[i].className = listName;
            }
            if (index >= 0 && index <= length - 1) {
                if (modelIndex.type == 12 && month.length == 0) {
                    index = modelIndex.menuIndex - 1;
                    elem[index].className = elem[index].className + ' menu-active';

                } else {
                    elem[index].className = elem[index].className + ' menu-active';

                }

            }

            var id = $(".focus_menu")[0].getAttribute('data-id');
            getMessage(id, 1)
            return me;
        }
        //赋值
    mod.initialization = function() {
        index = modelIndex.menuIndex;

    }

    mod.moveOp = function(directive) {
        // return
        var me = mod;
        console.log(directive, index, length)
        if (directive == 'backwards') { //左
            if (index > 0) {
                index--;
                me.toggleFocus();

            } else if (index == 0) {
                cmsIndex.handle('left');
                left.toggleFocus();
            }

        } else if (directive == 'forwards') { //右
            index = index < length - 1 ? ++index : length - 1
            me.toggleFocus();
        } else if (directive == 'down') {
            //      	me.cleanCSS();
            result = cmsIndex.handle('right');
            result.toggleFocus(0);
        }
        console.log(index)

        return me;
    }

    mod.grabEvent = function(key) {
        var me = mod;
        var keyJson = TV.GetKey(key);
        if (keyJson.keyState && keyJson.keyState != 'enter' && keyJson.keyState != 'back') {
            me.moveOp(keyJson.keyState);
        }
        return keyJson.isBlock;
    };

    return mod;
})();

cmsIndex.run();